///////////////////////////////////////////////////////////
//  CCreateChoiceView.cpp
//  Implementation of the Class CCreateChoiceView
//  Created on:      21-Sep-2013 9:37:40 AM
//  Original author: Jakkaphan Aiemcharoenjit
///////////////////////////////////////////////////////////

#include "CCreateChoiceView.h"
#include "stdafx.h"

using namespace UI;

CCreateChoiceView::CCreateChoiceView(IWindowDirector* director)
{
  this->m_type = IViewCtrl::CREATECHOICEVIEW_T;
  this->m_parrentWnd = director;
  this->m_choices = NULL;
  this->m_currentRow = -1;

  // call initialize for create all UI.
  Initialize();
}

CCreateChoiceView::~CCreateChoiceView()
{

}

UI::IViewCtrl::ViewType CCreateChoiceView::GetType() const 
{
	return  this->m_type;
}


void CCreateChoiceView::Initialize()
{
  this->SetAttribute<int>("x1", 0);
  this->SetAttribute<int>("y1", 0);
  this->SetAttribute("width", "100%");
  this->SetAttribute("height", "90%");
  this->SetAttribute("backgroundColour", CColour(COLOUR_PAGE));
  this->SetEventHandler("clickview", this, &CCreateChoiceView::OnClickedView);

  // Create navigationbar and set right button name
  this->m_navigationBar = CreateNavigationBar(CAttributes()
    .Set("name", "navBar")
    .Set("title", "Create choices")
    .Set("leftButtonCaption", "Back")
    .Set("x1",      0)
    .Set("y1",      0)
    .Set("width",   "100%")
    .Set("height",  "10%"));

  // Setting event handler for handling navigation bar click
  this->m_navigationBar->SetEventHandler("clicknavigationbar", this, &CCreateChoiceView::OnClickedNevigationBar);

  // Add navigation bar in view
  this->AddChild(this->m_navigationBar);

  // Create task label
  CLabelPtr textLable = CreateLabel(CAttributes()
    .Set("name",       "textLable")
    .Set("x1",         "2%")
    .Set("y1",         "15%")
    .Set("width",      "92%")
    .Set("height",     "7%")
    .Set("caption",    "Name ")
    .Set("fontSize",   "medium")
    .Set("fontAlignH", "left")
    .Set("fontAlignV", "middle")
    .Set("fontColour", CColour(COLOUR_LABEL)));

  // Add text label in view
  this->AddChild(textLable);

  // Create choice text field
  this->m_choiceTextField = CreateTextField(CAttributes()
    .Set("name",    "TextField1")
    .Set("x1",      "10%")
    .Set("y1",      "25%")
    .Set("width",   "90%")
    .Set("height",  "10%")
    .Set("text",    "")
    .Set("fontSize",    "medium")
    .Set("fontColour",  CColour(COLOUR_BLACK))
    .Set("backgroundColour", CColour(COLOUR_WHITE))
    .Set("fontAlignH",  "left"));

  // Add choice text field in view
  this->AddChild(this->m_choiceTextField);

  // Create add button
  CButtonPtr button = CreateButton(CAttributes()
    .Set("name",    "addbutton")
    .Set("caption", "Add")
    .Set("x1",      "50%")
    .Set("y1",      "38%")
    .Set("width",   "25%")
    .Set("height",  "10%"));

  button->SetEventHandler("click", this, &CCreateChoiceView::OnClickedAdd);

  // Add button in view
  this->AddChild(button);

  button = CreateButton(CAttributes()
    .Set("name",    "deletebutton")
    .Set("caption", "Delete")
    .Set("x1",      "75%")
    .Set("y1",      "38%")
    .Set("width",   "25%")
    .Set("height",  "10%"));

  button->SetEventHandler("click", this, &CCreateChoiceView::OnClickedDelete);

  // Add button in view
  this->AddChild(button);

  // Create table
  this->m_table = CreateTable(CAttributes()
    .Set("name",    "TablePages")
    .Set("x1",      0)
    .Set("y1",      "50%")
    .Set("width",   "100%")
    .Set("height",  "50%")
    .Set("alignH",  "top"));

  // Setting event handler for handling table selected row
  this->m_table->SetEventHandler("selectrow", this, &CCreateChoiceView::OnSelectedTable);

  // Add table in view
  this->AddChild(this->m_table);
}


void CCreateChoiceView::Refresh()
{
  // Create choice item
  int iChioces = this->m_choices->CountChoices();

  int iRows = (int)this->m_tableRows.size();
  int index = 0;
  // Remove all row in table
  for(index = 0; index < iRows; index++)
    this->m_table->RemoveChild(this->m_tableRows[index]);

  // **** Find the method to delete auto pointer ****
  this->m_tableRows.clear();

  for(index = 0; index < iChioces; index++)
  {
    char tmp[0xff];
    snprintf(tmp, 0xff, "LabelTable%d", index);

    AppendTableItem(tmp, this->m_choices->GetChoice(index).c_str());
  }
}


void CCreateChoiceView::ShowView(bool show)
{
  this->SetAttribute("visible", show);
}


bool CCreateChoiceView::OnClickedAdd(CButton* button)
{
  CString text;
  this->m_choiceTextField->GetAttribute("text", text);
  if(!std::string(text.Get()).empty())
  {
    char tmp[0xff];
    snprintf(tmp, 0xff, "LabelTable%d", this->m_choices->CountChoices());

    AppendTableItem(tmp, text.Get());

    this->m_choices->AddChoice(text.Get());
    this->m_choiceTextField->SetAttribute("text", "");
  }
  return true;
}


void CCreateChoiceView::OnClickedBack()
{

}


bool CCreateChoiceView::OnClickedDelete(CButton* button)
{
  if(this->m_currentRow != -1)
  {
    //this->m_choices->RemoveChoice(this->m_currentRow);
    //Refresh();
    DeleteTableItem(this->m_currentRow);
    this->m_currentRow = -1;
  }
  return true;
}

void CCreateChoiceView::SetChoices(IChoices* data)
{
  this->m_choices = data;
}

bool UI::CCreateChoiceView::OnSelectedTable( CTable* table, int tab )
{
  this->m_currentRow = tab;
  return true;
}

bool UI::CCreateChoiceView::OnClickedNevigationBar( CNavigationBar* navigationBar, int button )
{
  switch(button)
  {
  case S3E_NUI_NAVIGATION_BAR_BUTTON_LEFT:
    {
      this->m_parrentWnd->OnShowView(this, IViewCtrl::CREATETEMPLATEVIEW_T);
    }
    break;
  case S3E_NUI_NAVIGATION_BAR_BUTTON_RIGHT:
    break;
  default: break;
  }
  return true;
  return true;
}

bool UI::CCreateChoiceView::OnClickedView( CView* view )
{
  return this->m_parrentWnd->HideKeyboard();
}

void UI::CCreateChoiceView::AppendTableItem( const char* name, const char* caption )
{
  CTableItemPtr tableItem = CreateTableItem(CAttributes()
    .Set("width",   "100%")
    .Set("height",  "50"));

  CLabelPtr label = CreateLabel(CAttributes()
    .Set("name",    name)
    .Set("caption", caption)
    .Set("width", "100%")
    .Set("height", "100%")
    );

  tableItem->AddChild(label);

  this->m_table->AddChild(tableItem);

  this->m_tableRows.push_back(tableItem);
}

void UI::CCreateChoiceView::DeleteTableItem( int index )
{
  this->m_choices->RemoveChoice(this->m_currentRow);
  this->m_table->RemoveChild(this->m_tableRows[index]);
}
